package org.edu.modules.growth.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.growth.config.EvaluateResultTypeEnum;
import org.edu.modules.growth.dto.VeJsczEvaluateDTO;
import org.edu.modules.growth.entity.VeJsczEvaluate;
import org.edu.modules.growth.service.IVeJsczEvaluateService;
import org.edu.modules.growth.vo.DictVo;
import org.edu.modules.growth.vo.QueryCondition;
import org.edu.modules.growth.vo.TeacherBaseInfoVo;
import org.edu.modules.growth.vo.VeJsczEvaluateExcelVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Api(tags="年终考核管理")
@RestController
@RequestMapping("jsczEvaluate")
@Slf4j
@AllArgsConstructor
public class VeJsczEvaluateController extends BaseController<VeJsczEvaluate, IVeJsczEvaluateService> {

    private IVeJsczEvaluateService mIVeJsczEvaluateService ;

    @ApiOperation(value = "(开始年终考核页面)待考核教师列表初始化--未实现")
    @GetMapping("/teacherInit")
    public Result<?> teacherInit( ) {
        return Result.OK();
    }

    @ApiOperation(value = "考核评定基本列表")
    @GetMapping("/getDict")
    public Result<List<DictVo>> getDict( ) {
        return Result.OK(EvaluateResultTypeEnum.getDict());
    }

    @ApiOperation(value = "获取教师基本信息（教师成长档案、教学事故、考核结果、评教结果等）")
    @GetMapping("/baseInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "teacherUserId", value = "教师用户Id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "yearCode", value = "学年", dataType = "String")
    })
    public Result<TeacherBaseInfoVo> baseInfo( @RequestParam("teacherUserId") String teacherUserId,
                                               @RequestParam("yearCode") String yearCode) {
        return Result.OK(mIVeJsczEvaluateService.getInfo(Long.parseLong(teacherUserId),yearCode));
    }



    /**
     * 提交或修改数据
     */
    @ApiOperation(value = "提交或修改数据")
    @PostMapping("/addOrUpdate")
    public Result<?> addOrUpdate(@RequestBody VeJsczEvaluateDTO dto) {
        VeJsczEvaluate v = new VeJsczEvaluate() ;
        if( v.getId() != null ){
            v.setResult(dto.getResult()).setSuggest(dto.getSuggest())
                    .setId(dto.getId()) ;
            mIVeJsczEvaluateService.updateById(v) ;
        }else{
            BeanUtils.copyProperties(dto,v);
            Map<String,Object> map = Maps.newHashMap() ;
            map.put("teacherUserId",dto.getTeacherUserId()) ;
            map.put("yearCode",dto.getYearCode()) ;
            List<VeJsczEvaluate> list = mIVeJsczEvaluateService.listByMap(map) ;
            if( !CollectionUtils.isEmpty(list)){
                return Result.error("该教师的年终考核记录已存在！") ;
            }
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            v.setExamTime(System.currentTimeMillis()/1000).setTerminalId(1L) ;
//                    .setExamUserId(Long.parseLong(user.getId())) ;
            mIVeJsczEvaluateService.save(v) ;
        }
        return Result.OK();
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/selectPage")
    public Result<?> selectPage(@RequestBody QueryCondition queryCondition ) {
        return Result.OK(mIVeJsczEvaluateService.selectPage(queryCondition));
    }

    /**
     * 根据id详情
     */
    @ApiOperation(value = "查询（根据结果id--仅获取考核结果信息）")
    @GetMapping("/getDetail")
    public Result<VeJsczEvaluate> getDetail(@RequestParam("id") Long id) {
        return Result.OK(mIVeJsczEvaluateService.getById(id));
    }


    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @PostMapping("/removeById")
    public Result<?> removeById(@RequestParam("id") Long id) {
        return Result.OK(mIVeJsczEvaluateService.removeById(id) );
    }

    /**
     * 批量删除
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/removeByIds")
    public Result<?> removeByIds(@RequestBody List<Long> ids) {
        if( !CollectionUtils.isEmpty(ids) ){
            return Result.OK(mIVeJsczEvaluateService.removeByIds(ids) );
        }
        return Result.error( "传输参数为空！" );
    }


    @ApiOperation(value = "年终考核统计--未实现")
    @GetMapping ("/yearEndStatic")
    public Result<?> yearEndStatic() {

        return Result.OK();
    }

    /**
     * 导出
     */
    @AutoLog(value = "导出excel表格")
    @ApiOperation(value = "导出excel表格")
    @PostMapping(value = "export")
    public ModelAndView excelExport(@RequestParam("yearCode")Integer yearCode,
                                    @RequestParam(value = "JG",required = false)String JG
                                    ,@RequestParam(value = "XM",required = false)String XM,
                                    @RequestParam(value = "GH",required = false)String GH) throws Exception {
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        // 查询数据
        List<VeJsczEvaluateExcelVo> list = mIVeJsczEvaluateService.exportData(yearCode,JG,XM,GH) ;
        if( !CollectionUtils.isEmpty(list)){
            mv.addObject(NormalExcelConstants.FILE_NAME, "年终考核信息_"+yearCode);
            mv.addObject(NormalExcelConstants.CLASS, VeJsczEvaluateExcelVo.class);
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            ExportParams exportParams = new ExportParams("年终考核信息", "导出人:" , "年终考核信息");
            mv.addObject(NormalExcelConstants.PARAMS, exportParams);
            mv.addObject(NormalExcelConstants.DATA_LIST, list);
            return mv;
        }else{
            throw new Exception("没有数据！") ;
        }

    }


    @ApiOperation(value = "我的考核结果")
    @GetMapping("/myEvaluate")
    public Result<VeJsczEvaluate> myEvaluate(@RequestParam("yearCode") Long yearCode) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Map<String,Object> map = Maps.newHashMap() ;
        map.put("teacherUserId",user.getId());
        map.put("yearCode",yearCode);
        List<VeJsczEvaluate> list = mIVeJsczEvaluateService.listByMap(map) ;
        if( !CollectionUtils.isEmpty(list)){
            return Result.OK(list.get(0));
        }
        return Result.OK();
    }


}
